package dao;

import dao.inte.PasswordResetRepository;
import domain.PasswordReset;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Singleton;
import javax.persistence.NoResultException;
import javax.persistence.Query;

@Singleton
public class PasswordResetRepositoryImpl extends AbstractEntityRepository<PasswordReset> implements PasswordResetRepository {
    public PasswordResetRepositoryImpl() {
        super(PasswordReset.class);
    }


    @Override
    protected Class<PasswordReset> getEntityClass() {
        return PasswordReset.class;
    }

    @Transactional(noRollbackFor = NoResultException.class)
    public PasswordReset findByKey(String key) {
        PasswordReset o = null;
        try {
            Query q = entityManager.createQuery("from PasswordReset u where u.randomKey = :key", PasswordReset.class);
            q.setParameter("key", key);
            o = (PasswordReset) q.getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
        return o;
    }
}